require('es6-promise').polyfill();   // 使用 es6-promise 作为缺省的Promise.
require('isomorphic-fetch');         // 使用 isomorphic-fetch 作为缺省的Fetch.
//
import './_style/common.less';
// import './_style/antd.css';
//
import React from 'react';
import ReactDom from 'react-dom';
import { Router, Route, IndexRoute, Redirect, hashHistory } from 'react-router';
import routes from './routes'
import { Provider } from 'react-redux';
import reducers from './reducers';
import promise from 'redux-promise';
import { createStore, applyMiddleware } from 'redux';
import axios from 'axios';
import { API_ROOT } from './config.jsx';

import { SELECT_ARTICLE_DATA, FETCH_ARTICLE_DATA, INIT_SEL_ARTICLE_DATA, FETCH_FAVORITE_DATA, FETCH_APPROVE_DATA, RESOLVE_APPROVE_DATA, REJECT_APPROVE_DATA} from './actions/article';
import { FETCH_DETAIL, POST_VOTE, POST_COMMENT, POST_SURVEY } from './actions/detail';
import { FETCH_HOME_DATA, SWITCH_PORTAL, SWITCH_CHANNEL, SWITCH_ARTICLE,CHANNEL_LOADMORE } from './actions/home';
import { USER_SIGN, USER_REGISTER, USER_GET_USERINFO, USER_GET_HOSPITAL } from './actions/sign';
import { GET_CONFERENCE } from './actions/conference';

window.addEventListener("hashchange",function(event){
    var path = document.location.hash;
    var index = path.indexOf('?');
    path = path.substring(0,index);
    console.log(path);
    if(path.indexOf('#/')) {
        path = path.substring(1,index);
        _hmt.push(['_trackPageview', path]);
    }
});

// Comment for middleware to check network error or promission error
const errorChecker = store => next => action => {
    if (action.error && action.type != USER_GET_HOSPITAL) {
        hashHistory.push('500error');
    } else {
        switch (action.type) {
            case SELECT_ARTICLE_DATA:
            case FETCH_ARTICLE_DATA:
            case FETCH_FAVORITE_DATA:
            case FETCH_DETAIL:
            case POST_VOTE:
            case POST_COMMENT:
            case POST_SURVEY:
            case FETCH_HOME_DATA:
            case SWITCH_PORTAL:
            case SWITCH_CHANNEL:
            case SWITCH_ARTICLE:
            case FETCH_APPROVE_DATA:
            case CHANNEL_LOADMORE:
            case RESOLVE_APPROVE_DATA:
            case REJECT_APPROVE_DATA:
            case GET_CONFERENCE:
                if (action.payload.data.status === 401) { 
                    if (action.type == FETCH_ARTICLE_DATA ) {
                        hashHistory.push('register');
                    } else {
                        hashHistory.push('500error');
                    }
                } else if (action.payload.status === 200){
                    // sessionStorage.token = action.payload.headers['token'];
                    next(action);
                }
                return;
            case USER_GET_USERINFO:
                if (action.payload.status === 200) {
                    sessionStorage.token = action.payload.data.user.Token;
                    if (action.payload.data.user.UserInfo != undefined) {
                        sessionStorage.nickname = action.payload.data.user.UserInfo.nickname;
                        sessionStorage.headimgurl = action.payload.data.user.UserInfo.headimgurl;
                    } else {
                        sessionStorage.nickname = undefined;
                        sessionStorage.headimgurl = undefined;
                    }
                    if (action.payload.data.user.Roles&&action.payload.data.user.Roles.length>0) {
                        let str = action.payload.data.user.Roles.join(',');
                        str = str.toLowerCase();
                        if (str.indexOf('reviewer') > -1) {
                            sessionStorage.isReviewer = '1';
                        } else {
                            sessionStorage.isReviewer = '0';
                        }
                    }
                }
                return next(action);
            case USER_REGISTER:
                if (action.payload.status === 200) { 
                    sessionStorage.token = action.payload.data.user.Token;
                    if (sessionStorage.targetUrl && sessionStorage.targetUrl.length > 0 ) {
                        let targetUrl = sessionStorage.targetUrl
                        sessionStorage.removeItem('targetUrl');
                        hashHistory.push(targetUrl);

                    } else {
                        hashHistory.push('home');
                    }
                } 
                return next(action);
            // do something here
            default:
                return next(action);
        }
    } 
}


const createStoreWithMiddleware = applyMiddleware(
    promise,
    errorChecker
)(createStore);

var startup = function (DId) {
    return ReactDom.render(
        <Provider store={createStoreWithMiddleware(reducers) }>
            <Router history={hashHistory} routes={routes}/>
        </Provider>,
        document.getElementById(DId)
    );
};

module.exports = startup('react-content');
